//
//  CityViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/2.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire

let kScreenW : CGFloat = UIScreen.main.bounds.size.width
let kScreenH : CGFloat = UIScreen.main.bounds.size.height

class CityViewController: UIViewController,YZPullDownMenuDataSource {
    var numstring : String!
    var CityquArray = [quModel]()
    
    var bigCityquArray = [quModel]()
    
    
    var nameArray : [String] = ["市区","街道"]
    
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    
    var index = 1
    
    
     var tableView : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "区县版"
        self.view.backgroundColor = UIColor.white
      
        
        let name =  YZPullDownMenu()
        
        name.frame = CGRect(x: 0, y: 64, width: self.view.frame.size.width, height: 44)
        name.dataSource = self
        
        self.view .addSubview(name)
        
 
        
        self.setupAllChildViewController()
        
//        self.loadData()
        
        tableView = UITableView(frame:CGRect(x: 0, y: 108, width: self.view.frame.size.width, height: self.view.frame.size.height - name.frame.size.height), style:.grouped)
        
        tableView.dataSource = self
        tableView.delegate = self
        
        
        tableView.tableFooterView = UIView.init()
        
        self.view.addSubview(tableView)
        
        
        tableView.register(NSClassFromString("MainConversationingCell"), forCellReuseIdentifier: "MainConversationingCell")
        
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        // 现在的版本要用mj_header
        tableView.mj_header = header
        
        tableView.mj_header.beginRefreshing()
        
        // 上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        tableView.mj_footer = footer
    
    }
    // 顶部刷新
    @objc func headerRefresh(){
        self.bigCityquArray.removeAll()
        self.CityquArray.removeAll()
        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print(response)
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    //                    let model1 =   DynamicModel()
                    //
                    //
                    //                    model1.createtime = dataDic["createtime"].string
                    //
                    //                    model1.title = dataDic["title"].string
                    //
                    //                    model1.ids = dataDic["id"].string
                    //                    self.DynamicArray.append(model1)
                    //
                    //
                    //                    print("DynamicArray====\(self.DynamicArray.count)")
                    
                }
                
                
                 self.tableView.mj_header.endRefreshing()
                
            }else{
                 self.tableView.mj_header.endRefreshing()
            }
            
        }
        
    }
    //
    @objc func footerRefresh(){
        self.CityquArray.removeAll()
        
        index += 1
        
        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(index)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print(response)
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    //                    let model1 =   DynamicModel()
                    //
                    //
                    //                    model1.createtime = dataDic["createtime"].string
                    //
                    //                    model1.title = dataDic["title"].string
                    //
                    //                    model1.ids = dataDic["id"].string
                    //                    self.DynamicArray.append(model1)
                    //
                    //
                    //                    print("DynamicArray====\(self.DynamicArray.count)")
                    
                }
                
                
                 self.tableView.mj_footer.endRefreshing()
                
            }else{
                 self.tableView.mj_footer.endRefreshing()
            }
            
        }
        
        
    }
    func setupAllChildViewController()  {
        let quVC = quViewController()
        let xianVC = xianViewController()
        
        self.addChildViewController(quVC)
        self.addChildViewController(xianVC)
        
    }
    
    func numberOfCols(in pullDownMenu: YZPullDownMenu!) -> Int {
        return 2
    }
    
    func pullDownMenu(_ pullDownMenu: YZPullDownMenu!, buttonForColAt index: Int) -> UIButton! {
        let button = YZMenuButton(type: .custom)
        
        button.setTitle(nameArray[index], for: .normal)
        
        button.setTitleColor(UIColor.black, for: .normal)
        
        button.setImage(UIImage(named: "标签-向下箭头"), for: .normal)
        
        button.setImage(UIImage(named: "标签-向上箭头"), for: .selected)
        
        return button
        
    }
    
    func pullDownMenu(_ pullDownMenu: YZPullDownMenu!, viewControllerForColAt index: Int) -> UIViewController! {
        return self.childViewControllers[index]
    }
    
    func pullDownMenu(_ pullDownMenu: YZPullDownMenu!, heightForColAt index: Int) -> CGFloat {
        return 180
    }
 

    func loadData(){
        

        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print(response)
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
//                    let model1 =   DynamicModel()
//
//
//                    model1.createtime = dataDic["createtime"].string
//
//                    model1.title = dataDic["title"].string
//
//                    model1.ids = dataDic["id"].string
//                    self.DynamicArray.append(model1)
//
//
//                    print("DynamicArray====\(self.DynamicArray.count)")
                    
                }
                
               
          
                
                
            }else{
                
            }
            
        }
    }
    


}
extension CityViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 10
        
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 40
        
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    
    
    
    //返回分区头部视图
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        
        let headerView = UIView()
        headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 40)
        headerView.backgroundColor = UIColor.white
        let titleLabel = UILabel()
        titleLabel.text = "项目名称"
        titleLabel.textAlignment = .left
        titleLabel.textColor = UIColor.black
        titleLabel.font = UIFont.systemFont(ofSize:14)//一般方法
        titleLabel.sizeToFit()
        
        headerView.addSubview(titleLabel)
        
        
        let headerButton = UILabel()
        headerButton.font = UIFont.systemFont(ofSize:14)//一般方法
        headerButton.text = "项目进度"
        headerButton.textAlignment = .center
        headerButton.textColor = UIColor.black
        
        headerButton.sizeToFit()
        
        
        headerView.addSubview(headerButton)
        
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.top).offset(5)
            make.left.equalTo(headerView.snp.left).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        
        headerButton.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.top).offset(5)
            make.right.equalTo(headerView.snp.right).offset(-10)
            make.height.equalTo(30)
        }
        
        
        
        
        return headerView
        
        
    }
    //返回分区头部高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        
        return 40
        
        
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        
        let cell = MainConversationingCell(style:UITableViewCellStyle.default, reuseIdentifier: "MainConversationingCell")
        //
        //            let cellModel:ConversationingModel = self.ConversationingArrays[indexPath.row]
        //            cell.ConversationingWithModel(model: cellModel)
        return cell
        
        
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        
    }
    
}
